%% int sigma_t dW_t sigma_t=OU
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,332);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath; 
    for jj=1:51
        intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000).^2;
        %intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
%exactvar=3*time.^2;
hold on
%plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Monte Carlo')

%% int sigma_t dW_t sigma_t=W_t
tic
pathnum=100000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,331);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath; 
    for jj=1:51
        intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000).^2;
        %intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
%exactvar=3*time.^2;
hold on
%plot(time,exactvar);
plot(time,mcpart);
title('variance')
legend('Exact')

%% intW dt
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,32);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath; 
    for jj=1:51
        intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        %intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
%exactvar=3*time.^2;
hold on
%plot(time,exactvar);
plot(time,mcpart);
title('variance')
legend('Monte Carlo')

%% sigma_tW_t sigma_t=OU
tic
pathnum=50000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,312);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
%exactvar=3*time.^2;
hold on
%plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Monte Carlo')

%% sigma_tW_t sigma_t=W_t
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,311);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
exactvar=3*time.^2;
hold on
plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')
%% int sigma_tdW1
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,23);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
exactvar=3/2*time'+1/4*exp(-2*time)'-1/4;
hold on
plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')
%% int sigma_tdt
tic
pathnum=100000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,22);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        %intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
exactvar=mcpart;
hold on
plot(time,exactvar);
%plot(time,mcpart,'r');
title('variance')
legend('Exact')
%% int sigma_tdt
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,21);
    %getpath=ones(size(getpath));
    BMsq=getpath;   
    %BMsq=getpath.^2; 
    for jj=1:51
        intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        %intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
%exactvar=mcpart;
hold on
%plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Monte Carlo')

%% int sin(e^5t)dW
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,4);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
exactvar=zeros(51,1);
myfun=@(s) sin(exp(5*s)).^2;
for ii=2:51
    exactvar(ii)=quadl(myfun,0,time(ii));
end
hold on
plot(time,exactvar);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')

%% int sin(30t)dW
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,3);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
hold on
plot(time,time/2-sin(60*time)/120);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')

%% int sin(5t)dW
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=randomtest(5001,2);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
hold on
plot(time,time/2-sin(10*time)/20);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')


%% int 1dW
tic
pathnum=10000;
intBMsq=zeros(51,1);
time=linspace(0,1/2,51)';
for pn=1:pathnum
    disp(pn);
    getpath=ouprocess(0,0,0,1,5001);
    %getpath=ones(size(getpath));
    %BMsq=getpath;   
    BMsq=getpath.^2; 
    for jj=1:51
        %intBMsq(jj)=intBMsq(jj)+midptrule(BMsq(1:(jj-1)*100+1),1/10000)^2;
        intBMsq(jj)=intBMsq(jj)+BMsq((jj-1)*100+1);
    end
end
toc
mcpart=intBMsq/pathnum;
hold on
plot(time,time);
plot(time,mcpart,'r');
title('variance')
legend('Exact','Monte Carlo')